package com.xiaoyu.client.ui.activity.main.mine.my_order;

import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.xiaoyu.client.R;
import com.xiaoyu.client.network.NetworkConstants;
import com.xiaoyu.client.network.NetworkManager;
import com.xiaoyu.commonlib.network.CommonHttpResponser;
import com.xiaoyu.commonlib.ui.base.BaseActivityWithTitle;
import com.xiaoyu.commonlib.utils.CommonFragmentPagerAdapter;
import com.xiaoyu.commonlib.utils.ViewPagerUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author jx
 * @date 创建时间 2018/10/18
 * @Description 我的订单
 */
public class MyOrderActivity extends BaseActivityWithTitle {

    @BindView(R.id.activity_order_linear)
    LinearLayout mBarLinear;
    @BindView(R.id.activity_order_line)
    View mBarline;
    @BindView(R.id.activity_order_viewpager)
    ViewPager mViewPager;
    private CommonFragmentPagerAdapter mPageAdapter;
    private ViewPagerUtils mPagerUtils;
    private List<Fragment> fragments = new ArrayList<>();

    @Override
    protected void loadContentView(ViewGroup parent) {
        View mView = LayoutInflater.from(this).inflate(R.layout.activity_my_order, parent, true);
        setTitleText("我的订单");
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {

        mViewPager.addOnPageChangeListener(pageChangeListener);
        for (int i = 0; i < mBarLinear.getChildCount(); i++) {
            mBarLinear.getChildAt(i).setOnClickListener(this);
        }

        mPagerUtils = new ViewPagerUtils(this, mBarLinear, mBarline);
        mPagerUtils.initLine(45);
        mPagerUtils.setTextColor("#333333", "#0c93e9");
        mPagerUtils.selectItem(0);

        for (int i = 0; i < 5; i++) {
            fragments.add(new OrderFragment().newInstance("order" + i));
        }

        mPageAdapter = new CommonFragmentPagerAdapter(getSupportFragmentManager(), fragments);
        mViewPager.setAdapter(mPageAdapter);
        mViewPager.setCurrentItem(0);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v == mBarLinear.getChildAt(0)) {

            mPagerUtils.selectItem(0);
            mPagerUtils.scrollLineMove(0, 0);
            mViewPager.setCurrentItem(0);
        }
        if (v == mBarLinear.getChildAt(1)) {

            mPagerUtils.selectItem(1);
            mPagerUtils.scrollLineMove(1, 0);
            mViewPager.setCurrentItem(1);
        }
        if (mBarLinear.getChildAt(2) == v) {

            mPagerUtils.selectItem(2);
            mPagerUtils.scrollLineMove(2, 0);
            mViewPager.setCurrentItem(2);
        }
        if (mBarLinear.getChildAt(3) == v) {

            mPagerUtils.selectItem(3);
            mPagerUtils.scrollLineMove(3, 0);
            mViewPager.setCurrentItem(3);
        }
        if (mBarLinear.getChildAt(4) == v) {

            mPagerUtils.selectItem(4);
            mPagerUtils.scrollLineMove(4, 0);
            mViewPager.setCurrentItem(4);
        }
    }

    /**
     * viewPager切换监听
     */
    private ViewPager.OnPageChangeListener pageChangeListener = new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            mPagerUtils.scrollLineMove(position, positionOffset);
        }

        @Override
        public void onPageSelected(int position) {
            mPagerUtils.selectItem(position);
            mViewPager.setCurrentItem(position);
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    };
}
